package cc.linker.steplink.crm.merchanth5.commons;

import cc.linker.steplink.crm.commons.Constants;
import cc.linker.steplink.crm.commons.coupon.CouponCustomerInfo;
import cc.linker.steplink.crm.commons.domain.WxUser;
import cc.linker.steplink.crm.response.GlobalException;
import cc.linker.steplink.crm.response.ResponseCode;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.TimeUnit;

/**
 * @Author: CKQ
 * @DateTime: 2020/6/13 13:10
 * @Description: TODO
 */

@Component
public class RequestFilter {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    /*
     * 获取登录用户信息
     * 如果未登录直接返回错误码告知前端，如果成功返回用户信息 WxUser对象
     * */
    public WxUser getWxUser(HttpServletRequest request) {
        String h5Token = request.getHeader(Constants.H5_TOKEN_NAME);
        if(StringUtils.isEmpty(h5Token)) {
            h5Token = request.getParameter("ht");
        }
        if (StringUtils.isEmpty(h5Token)) {
            throw new GlobalException(ResponseCode.UN_LOGIN);
        }
        String h5User = stringRedisTemplate.opsForValue().get("login:" + h5Token);
        if (StringUtils.isEmpty(h5User)) {
            // 严格校验，控制器不用考虑获取不到用户的情况
            throw new GlobalException(ResponseCode.UN_LOGIN);
        }
        WxUser wxUser = JSON.parseObject(h5User, WxUser.class);
        if (wxUser == null) {
            // 严格校验，控制器不用考虑获取不到用户的情况
            throw new GlobalException(ResponseCode.UN_LOGIN);
        }
        stringRedisTemplate.expire("login:" + h5Token, 15, TimeUnit.MINUTES);
        stringRedisTemplate.expire("SH5CHECK" + wxUser.getOpenid(), 15, TimeUnit.MINUTES);
        return wxUser;
    }

    /*
     * 获取登录用户信息不用登录
     * 如果未登录直接返回错误码告知前端，如果成功返回用户信息 WxUser对象
     * */
    public WxUser getWxUserNotLogin(HttpServletRequest request) {
        String h5Token = request.getHeader(Constants.H5_TOKEN_NAME);
        String h5User = stringRedisTemplate.opsForValue().get("login:" + h5Token);
        if (!StringUtils.isEmpty(h5User)) {
            WxUser wxUser = com.alibaba.fastjson.JSON.parseObject(h5User, WxUser.class);
            return wxUser;
        }
        return null;
    }

    /**
     * @Description：组装优惠券用户信息
     * @DateTime：2020/8/18 14:45
     * @Params：
     * @Return：
     */
    public CouponCustomerInfo getCouponCustomerInfo(HttpServletRequest request) {
        WxUser wxUser = this.getWxUser(request);
        CouponCustomerInfo customerInfo = CouponCustomerInfo
                .builder()
                .customerId(wxUser.getCustomerId())
                .customerName(wxUser.getNickname())
                .customerPhone(wxUser.getBindPhone())
                .cover("")
                .orgId(wxUser.getOrgId())
                .sourceUserId(wxUser.getSourceUserId())
                .build();
        return customerInfo;
    }


}
